﻿using System.Diagnostics;
using System.Windows.Documents;
using System.Windows.Navigation;

namespace RapidDict.Modules.Menu.Views
{
    using System;
    using System.Windows.Input;
    using Microsoft.Practices.Composite.Presentation.Commands;
    using Infrastructure.Interfaces;
    using Infrastructure.Models;

    public class MenuViewModel : ViewModelBase
    {
        private const string ImageSourcePin = "/RapidDict.Modules.Menu;Component/Images/pin-icon.png";
        private const string ImageSourceUnpin = "/RapidDict.Modules.Menu;Component/Images/unpin-icon.png";
        
        private readonly IAppViewService appViewService;
        private readonly IPronunciationService pronunciationService;
        private readonly ITranslationService translationService;
        private readonly ILanguagePairService languagePairService;

        private ICommand pinCommand;
        private ICommand exitCommand;
        private ICommand playSoundCommand;
        private ICommand hyperlinkClickCommand;

        private bool isPined;
        
        public MenuViewModel(IAppViewService appViewService, IPronunciationService pronunciationService, ITranslationService translationService, ILanguagePairService languagePairService)
        {
            this.appViewService = appViewService;
            this.pronunciationService = pronunciationService;
            this.translationService = translationService;
            this.languagePairService = languagePairService;
            this.isPined = this.appViewService.IsAlwaysOnTop();

            this.translationService.WordChanged += (s, e) => this.NotifyPropertyChanged("PlaySoundHelper");
        }

        public bool IsPined
        {
            get
            {
                return this.isPined;
            }

            set
            {
                this.Pin(null);
            }
        }

        public string PinImageSource
        {
            get
            {
                if (this.isPined)
                {
                    return ImageSourcePin;
                }

                return ImageSourceUnpin;
            }
        }

        public string PlaySoundHelper
        {
            get
            {
                return Guid.NewGuid().ToString();
            }
        }

        public ICommand PlaySoundCommand
        {
            get
            {
                if (this.playSoundCommand == null)
                {
                    this.playSoundCommand = new DelegateCommand<string>(this.Play, this.CanPlay);
                }

                return this.playSoundCommand;
            }
        }

        public ICommand PinCommand
        {
            get
            {
                if (this.pinCommand == null)
                {
                    this.pinCommand = new DelegateCommand<string>(this.Pin);
                }

                return this.pinCommand;
            }
        }

        public ICommand HyperlinkClickCommand
        {
            get
            {
                if (this.hyperlinkClickCommand == null)
                {
                    this.hyperlinkClickCommand = new DelegateCommand<string>(HyperlinkClick);
                }

                return this.hyperlinkClickCommand;
            }
        }

        public ICommand ExitCommand
        {
            get
            {
               if (this.exitCommand == null)
               {
                   this.exitCommand = new DelegateCommand<string>(this.Exit);
               }

               return this.exitCommand;
            }
        }

        private static void HyperlinkClick(string urlToGo)
        {
            Process.Start(new ProcessStartInfo(new Uri(urlToGo).AbsoluteUri));
        }

        private bool CanPlay(string arg)
        {
            return this.pronunciationService.IsPronounceable(this.translationService.GetWord(),
                                                             this.languagePairService.GetSourceLanguage());
        }

        private void Play(string obj)
        {
            this.pronunciationService.Pronounce(this.translationService.GetWord(), this.languagePairService.GetSourceLanguage());
        }

        private void Exit(string obj)
        {
            this.appViewService.CloseApp();
        }

        private void Pin(string obj)
        {
            this.appViewService.ChangeAlwaysOnTopMode();
            this.isPined = this.appViewService.IsAlwaysOnTop();
            this.NotifyPropertyChanged("PinImageSource");
            this.NotifyPropertyChanged("IsPined");
        }
    }
}
